package org.openspaces.example.helloworld.processor;

import net.jini.core.lease.Lease;

import org.openspaces.core.GigaSpace;
import org.openspaces.core.GigaSpaceConfigurer;
import org.openspaces.core.space.UrlSpaceConfigurer;
import org.openspaces.events.adapter.SpaceDataEvent;
import org.openspaces.example.helloworld.common.Coordinates;
import org.openspaces.example.helloworld.common.Direction;
import org.openspaces.example.helloworld.common.Roxel;
import org.openspaces.example.helloworld.common.StructureTuple;

import com.j_spaces.core.IJSpace;

import java.util.logging.Logger;


/**
 * The processor is passed interesting Objects from its associated PollingContainer
 * <p>The PollingContainer removes objects from the GigaSpace that match the criteria
 * specified for it.
 * <p>Once the Processor receives each Object, it modifies its state and returns it to
 * the PollingContainer which writes them back to the GigaSpace
 * <p/>
 * <p>The PollingContainer is configured in the pu.xml file of this project
 */
public class Processor {
	GigaSpace gigaSpace = null;
    Logger logger=Logger.getLogger(this.getClass().getName());

    /**
     * Process the given Message and return it to the caller.
     * This method is invoked using OpenSpaces Events when a matching event
     * occurs.
     */
    @SpaceDataEvent
    public StructureTuple buildStreets(StructureTuple structure) {
        logger.info("Processor PROCESSING: " + structure);
        
        for (int x = 0; x < structure.getBlocksX(); x++) {
        	for (int y = 0; y < structure.getBlocksY(); y++) {
				fillBlock(x, y, structure);
			}
		}
		structure.setProcessed(true);
		return structure;
	}
    
//    private void fillBlock(int x, int y, StructureTuple struc) {
//	    int blockCenter = struc.getBlockLen() / 2;
//	    for (int i = 0; i < struc.getBlockLen(); i++) {
//		    for (int j = 0; j < struc.getBlockLen(); j++) {
//			    if (i == blockCenter && j == blockCenter) {
//			    	gigaSpace.write(new Roxel(struc.getRoxelLen(), x*struc.getBlockLen() + i, y*struc.getBlockLen() + j, Direction.TODECIDE, x, y));
//			    } else if (i == blockCenter) {
//			    	gigaSpace.write(new Roxel(struc.getRoxelLen(), x*struc.getBlockLen() + i, y*struc.getBlockLen() + j, Direction.SOUTH, x, y));
//			    } else if (j == blockCenter) {
//			    	gigaSpace.write(new Roxel(struc.getRoxelLen(), x*struc.getBlockLen() + i, y*struc.getBlockLen() + j, Direction.EAST, x, y));
//			    }
//		    }
//	    }
//    }
    
    private void fillBlock(int x, int y, StructureTuple struc) {
	    int blockCenter = struc.getBlockLen() / 2;
	    for (int i = 0; i < struc.getBlockLen(); i++) {
		    for (int j = 0; j < struc.getBlockLen(); j++) {
			    if (i == blockCenter && j == blockCenter) {
			    	gigaSpace.write(new Roxel(struc.getRoxelLen(), i, j, false,true,true,false,true, x, y));
			    } else if (i == blockCenter) {
			    	gigaSpace.write(new Roxel(struc.getRoxelLen(), i, j, false,false,true,false,false, x, y));
			    } else if (j == blockCenter) {
			    	gigaSpace.write(new Roxel(struc.getRoxelLen(), i, j, false,true,false,false,false, x, y));
			    }
		    }
	    }
    }

	public Processor() {
        logger.info("Processor instantiated, waiting for messages feed...");
        // connect to the space using its URL
        IJSpace space = new UrlSpaceConfigurer("jini://*/*/processorSpace").space();
        // use gigaspace wrapper to for simpler API
        gigaSpace = new GigaSpaceConfigurer(space).gigaSpace();
        
    }

}
